package net.sparrow.mcaccess;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import reifnsk.minimap.ReiMinimap;
import reifnsk.minimap.Waypoint;

public class ReiAccessService {


	private List<Waypoint> addlWaypoints = new ArrayList<Waypoint>();
	private List<Waypoint> realWaypoints;
	public static List<String> hackReiList = Arrays.asList("allowCavemap","allowEntitiesRadar",
			"allowEntityPlayer","allowEntityAnimal","allowEntityMob","allowEntitySquid","allowEntitySlime","allowEntityLiving");
	
	
	public ReiAccessService() {
		//grab the waypoints list
		Class minimapClass = ReiMinimap.class;
		try {
			Field realWpts = minimapClass.getDeclaredField("wayPts");
			realWpts.setAccessible(true);
			realWaypoints = (List<Waypoint>) realWpts.get(ReiMinimap.instance);
		} catch (Exception e) {
			
		}
	}
	public void forceEntityRadar() {
		Class minimapClass = ReiMinimap.class;
		
		try {
			for (String fieldName : hackReiList) {
				Field allowField = minimapClass.getDeclaredField(fieldName);
				allowField.setAccessible(true);
				allowField.set(ReiMinimap.instance, true);
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
	
	public void addWaypoint(String name, int x, int y, int z) {
		for(Waypoint w : addlWaypoints) {
			if(w.name.equals(name)) { //if this waypoint already exists, update it instead
				w.x = x;
				w.y = y;
				w.z = z;
				return;
			}
		}
		Waypoint w = new Waypoint(name, x, y, z, true, (float)Math.random(),
				(float)Math.random(), (float)Math.random());
		addlWaypoints.add(w);
		realWaypoints.add(w);
	}
	
	public void removeWaypoint(String name) {
		Waypoint fndW = null;
		for(Waypoint w : addlWaypoints) {
			if(w.name.equals(name)) {
				fndW = w;
			}
		}
		if(fndW != null) {
			addlWaypoints.remove(fndW);
			realWaypoints.remove(fndW);
		}
	}
}
